package com.viroyal.monitorlib;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import com.viroyal.monitorlib.network.Api;
import com.viroyal.monitorlib.network.HttpManager;
import com.viroyal.monitorlib.network.SslConfig;
import com.viroyal.monitorlib.utils.CrashHandler;
import com.viroyal.monitorlib.utils.MyActivityManager;
import org.kymjs.kjframe.KJDB;

/**
 * Created by cxy_nj on 2016/12/27.
 */

public class UpdateApp extends Application {
    private static final String TAG = "UpdateApp";
    private static Context context;

    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
        context = getApplicationContext();
        AppModel.init(this);
        HttpManager.init(true, true ? SslConfig.CER_139_196_60_123 : SslConfig.CER_139_196_251_205);
        Api.init(this);
        KJDB.create(this, "update.db", true);

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                MyActivityManager.getInstance().setCurrentActivity(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

//    @Override
//    public void onTrimMemory(int level) {
//        super.onTrimMemory(level);
//        back();
//    }

//    private void back() {
//        if (AppUtils.isBackground(this)) {
//            //将应用调用至前台
//            ActivityManager activtyManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
//            List<ActivityManager.RunningTaskInfo> runningTaskInfos = activtyManager.getRunningTasks(100);
//            for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTaskInfos) {
//                if (this.getPackageName().equals(runningTaskInfo.topActivity.getPackageName())) {
//                    activtyManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
//                    return;
//                }
//            }
//        }
//    }
}
